#include <avr/io.h>
#include "timer.h"
#include "os.h"
#include "kernel.h"
#include <avr/interrupt.h>


/**
 * \file timer.c
 * \author Clecio Varjao and Leanne Ross
 * \date October 2007
 * \remarks Based on code created by Dr. Mantis Cheng
 * \brief RTOS timer implementation
 */


/**
 *	Hardware initialization of timer
 */
void kernel_init_timer(){
	//8MHz => 8 million cycles per scond
   TIMSK1 = 0;                                              // Disable timer interrupts 
   TCCR1A = 0;                                              // Configure the initial timer properties
   TCCR1B = (_BV(WGM12)) | (_BV(CS12)) | (_BV(CS10));       // Configure the initial timer properties
   TCNT1  = 0;   
} // end-function

/**
 *  Hardware enable of timer interrupts
 */
void kernel_start_timer(){
   OCR1A = TIMER_FACTOR;							// Init the counter to the first time. (5x8)
   TIMSK1 |= _BV(OCIE1A);							// Enable interrupts    
   TCNT1 = 0;
} // end-function

/**
 *  Interrupt Service Routine
 */
ISR (TIMER1_COMPA_vect) {
	kernel_tick();
} // end-function
